
#ifndef Server_H
#define Server_H



#include <string>

#include <algorithm>
#include <queue>

#include "Runnable.h"
#include "XmlServer.h"
#include "types.h"
#include "RemoteServer.h"
#include <list>
#include "ServerServerMsgs.h"
#include "ClientServer.h"
#include "Coordinator.h"
#include "RemoteCoordinator.h"
#include "ServerCoordinator.h"
#include "TaskProcess.h"
#include "boost\shared_ptr.hpp"
#include "PrezenterServer.h"

typedef boost::shared_ptr<TaskProcess> TaskProcessPtr; 

class ServerUnWraper;
class XmlServer;
class Coordinator;

class Server : public Runnable { 
public :

	enum ServerMode {WORKING_MODE, ELECTION_MODE };
    Server(int ID, int port, const std::string& name,const std::string & myAddress);
    Server();
    ~Server();

	void startServer();
	void startServer(const ServerInfo &);

	void startCoordinator();
	void stopCoordinator();
	void updateServerList(const ServerInfo&);
	
    void run();
    std::string getName() const;
    int getPort() const;
    void setPort(int p_port);    
	void connect(const ServerInfo &);
	void election(const ElectionMsg & elct);
	void doElection();
	void alive();
	void setMode(const ServerMode&);
	void taskStatusChange();
	void serverCheck(AliveRet aliveRet);
	ServerMode getMode();
	RegistrationRet registration(const Registration & regMsg);
	ElectedRet elected(const Elected& electedMsg);
	TaskRet addTask(const TaskMsg& task);
	TaskRet getTaskInfo(const TaskCheckMsg & taskCheck);
	CordinatorAddresRet getCoordintorAddres();
	
	//XmlServer* getItsXmlServer() const;

protected :
	ServerInfo serverInfo;
	ServerMode serverMode;
	int ID;
    void cleanUpRelations();
    std::string name;	
    int port;
	std::string myAddress;
	std::list<RemoteServer*> remoteServerList;
	RemoteCoordinator *remoteCoordinator;
	std::list<Task> myTaskList;
	std::list<TaskProcessPtr> taskProcessList;
    ServerUnWraper* itsServerUnWraper;
    XmlServer* itsXmlServer;
	boost::thread * rpcServerThread;
	boost::thread * serverThread;

	bool activeCoordinator;
	Coordinator * coordinator;
	int maxID;
	int coordinatorPort;

};

#endif
/*********************************************************************
	File Path	: DefaultComponent\DefaultConfig\Server.h
*********************************************************************/
